package com.cjack.frenchexcel.service;

import com.cjack.frenchexcel.dao.IntegralRecordDao;
import com.cjack.frenchexcel.model.IntegralRecordModel;
import com.cjack.frenchexcel.table.IntegralRecordTable;
import com.cjack.frenchexcel.util.ModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 后台账户管理
 * Created by root on 10/5/19
 */
@Service
public class IntegralRecordService {

    @Autowired
    private IntegralRecordDao dao;

    public List< IntegralRecordModel> list( IntegralRecordModel model){
        Sort sort = new Sort( Sort.Direction.DESC, "updateTime");
        Example<IntegralRecordTable> example = Example.of( ModelUtils.copySignModel( model, IntegralRecordTable.class));
        return ModelUtils.copyListModel( dao.findAll( example, sort), IntegralRecordModel.class);
    }

    public void del( String ids){
        List< IntegralRecordTable> tables = new ArrayList<>();
        String[] idArr = ids.split( ",");
        for( String id : idArr){
            IntegralRecordTable table = new IntegralRecordTable();
            table.setId( Long.valueOf( id));
            tables.add( table);
        }
        dao.deleteInBatch( tables);
    }

    public void save( IntegralRecordModel model){
        dao.save( ModelUtils.copySignModel( model, IntegralRecordTable.class));
    }

    public IntegralRecordTable get(Long id) {
        return dao.findOne( id);
    }
}
